package com.xh.one2one.lesson.service.impl;

import com.xh.bussiness.tiku.bean.dto.PaperDto;
import com.xh.bussiness.tiku.service.IPaperService;
import com.xh.core.util.UserUtils;
import com.xh.one2one.lesson.bean.request.LessonHomeworkRequest;
import com.xh.one2one.lesson.bean.request.LessonTimesRequest;
import com.xh.one2one.lesson.model.LessonHomework;
import com.xh.one2one.lesson.dao.LessonHomeworkMapper;
import com.xh.one2one.lesson.model.LessonInfo;
import com.xh.one2one.lesson.model.LessonPlan;
import com.xh.one2one.lesson.model.LessonTimes;
import com.xh.one2one.lesson.service.ILessonHomeworkService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.one2one.lesson.service.ILessonInfoService;
import com.xh.one2one.lesson.service.ILessonPlanService;
import com.xh.one2one.lesson.service.ILessonTimesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 一对一备课作业 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-14
 */
@Service
public class LessonHomeworkServiceImpl extends BaseServiceImpl<LessonHomeworkMapper, LessonHomework> implements ILessonHomeworkService {

    @Autowired
    private ILessonInfoService lessonInfoService;
    @Autowired
    private ILessonTimesService lessonTimesService;
    @Autowired
    private IPaperService paperService;
    @Autowired
    private ILessonPlanService lessonPlanService;

    @Override
    public XhPage queryLessonHomeworkPage(XhPage page, LessonHomeworkRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonHomework>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryLessonHomework(LessonHomeworkRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonHomework>();
        //拼装条件
        qw=prepareParams(bean, qw);


        return this.dao.selectList(qw);
    }

    @Override
    public LessonHomework queryLastByLessonId(LessonHomeworkRequest bean) {
//        QueryWrapper lessonQuery = new QueryWrapper<LessonInfo>();
        boolean isOne = 1 == bean.getType();
//        if(isOne){、
//            lessonQuery.eq("contract_id",bean.getLessonId());
//        }else{
//            lessonQuery.eq("lesson_id",bean.getLessonId());
//        }
        LessonPlan lessonPlan = lessonPlanService.getById(bean.getLessonId());
        QueryWrapper<LessonPlan> qw = new QueryWrapper();
//        qw.eq("subject_id", lessonPlan.getSubjectId());
//        qw.eq("teacher_id", lessonPlan.getTeacherId());
        qw.eq("mark_id", lessonPlan.getMarkId());
        qw.ne("id", lessonPlan.getId());
        qw.lt("lesson_time", lessonPlan.getLessonTime());
        qw.orderByDesc("lesson_time");
        LessonPlan pre = lessonPlanService.selectFirst(qw);
        if(pre == null){
            return null;
        }
        LessonInfo lastLessonInfo = lessonInfoService.getByThirdId(pre.getId());
//        lessonQuery.eq("creator_id", UserUtils.getCurrentUserId());
//        lessonQuery.orderByAsc("start_time");
//        List<LessonInfo> lessonInfos = lessonInfoService.queryByCondition(lessonQuery);
//        LessonInfo lastLessonInfo = null;
//        for (int i = 0; i < lessonInfos.size(); i++) {
//            LessonInfo lessonInfo = lessonInfos.get(i);
//            if(lessonInfo.getThirdId().equals(bean.getLessonThirdId())){
//                if(i == 0){
//                    break;
//                }
//                lastLessonInfo = lessonInfos.get(i-1);
//                break;
//            }
//        }
        if(null == lastLessonInfo){
            return null;
        }
        LessonHomeworkRequest request = new LessonHomeworkRequest();
        request.setLessonTimeId(lastLessonInfo.getId());
        LessonHomework lessonHomework = queryOne(request);
        if(null == lessonHomework){
            LessonTimesRequest timesRequest = new LessonTimesRequest();
            timesRequest.setLessonId(lastLessonInfo.getLessonId());
            if(isOne){
                timesRequest.setLessonId(lastLessonInfo.getContractId());
            }
            timesRequest.setLessonThirdId(lastLessonInfo.getThirdId());
            List<LessonTimes> lessonTimes = lessonTimesService.queryLessonTimes(timesRequest);
            if(!CollectionUtils.isEmpty(lessonTimes)){
                LessonTimes times = lessonTimes.get(0);
                request.setLessonTimeId(times.getId());
                lessonHomework = queryOne(request);
            }
        }
        return lessonHomework;
    }


    public LessonHomework queryOne(LessonHomeworkRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonHomework>();
        //拼装条件
        qw=prepareParams(bean, qw);
        return this.dao.selectOne(qw);
    }
    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(LessonHomeworkRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!StringUtils.isEmpty(bean.getLessonTimeId())){
            qw.eq("lesson_time_id",bean.getLessonTimeId());
        }
        return qw;
    }

    /**
     * 查询巩固练习
     * @author 赵文博
     * @date 2021/6/8 18:15
     * @param bean
     * @return com.xh.bussiness.tiku.bean.dto.PaperDto
     **/
    @Override
    public PaperDto queryHomeworkQuestion(LessonHomeworkRequest bean){
        QueryWrapper<LessonTimes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lesson_third_id",bean.getLessonThirdId());
        LessonTimes lessonTimes = lessonTimesService.selectFirst(queryWrapper,LessonTimes.class);
        String lessTimeId = "";
        if(lessonTimes!=null){
            lessTimeId = lessonTimes.getId();
        }else {
            QueryWrapper<LessonInfo> qw = new QueryWrapper<>();
            qw.eq("third_id",bean.getLessonThirdId());
            LessonInfo lessonInfo = lessonInfoService.selectFirst(qw);
            if(lessonInfo!=null){
                lessTimeId = lessonInfo.getId();
            }
        }
        QueryWrapper<LessonHomework> qwHomework = new QueryWrapper();
        qwHomework.eq("lesson_time_id",lessTimeId);
        LessonHomework lessonHomework = this.selectOne(qwHomework,LessonHomework.class);
        PaperDto paperDto = new PaperDto();
        if(lessonHomework!=null){
            paperDto = paperService.getPaper(lessonHomework.getPaperId());
        }

        return paperDto;
    }

    @Override
    public void deleteByRequestBean(LessonHomeworkRequest bean) {
        QueryWrapper<LessonHomework> queryWrapper = new QueryWrapper<>();
        prepareParams(bean,queryWrapper);
        deleteByCondition(queryWrapper);
    }
}
